<template>
  <view>
    <view class="search-fix">
      <MySearch @click="onSearch"></MySearch>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" indicator-color='#fff' :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        
        <view class="swiper-item">
          <!-- 导航，点击图片跳转 -->
          <navigator class="nav-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
            <image :src="item.image_src"></image>
          </navigator>
        </view>
        <!--  -->
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-cate">
      <view class="nav-content" v-for="(item, index) in navList" :key="index" @click="cateNavHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 商品楼层 -->
    <view class="floor-item" v-for="(item, index) in floorList" :key="index">
      <!-- 标题 -->
      <image :src="item.floor_title.image_src" class="floor-title"></image>
      <!-- 图片显示 -->
      <view class="img-visible">
        <view class="left-img">
          <!-- 点击楼层图片跳转到商品列表页 -->
          <image
          :src="item.product_list[0].image_src"
          :style="{width: item.product_list[0].image_width + 'rpx'}"
          ></image>
        </view>
        <view class="right-img">
          <navigator
          :style="{width: img.image_width + 'rpx'}"
          class="right-img-item"
          :url="img.url"
          v-for="(img, index) in item.product_list.slice(1)"
          :key="index"
          >
            <image
            :src="img.image_src"
            ></image>
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
   import MySearch from '../../components/my-search/my-search.vue'
  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiper()
      this.getCateNav()
      this.getFloorList()
    },
    methods:{
      // 获取轮播图图片地址
      async getSwiper() {
        const { data: {meta, message} } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if(meta.status !== 200) return uni.$showMsg()
        this.swiperList = message
      },
      // 获取分类导航数据
      async getCateNav() {
        const { data: {meta, message}  } = await uni.$http.get('/api/public/v1/home/catitems')
        if(meta.status !== 200) return uni.$showMsg()
        this.navList = message
      },
      // 点击分类导航处理
      cateNavHandler({name}) {
        if(name === '分类') {
          uni.switchTab({
            url: '../cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const { data: {meta, message}  } = await uni.$http.get('/api/public/v1/home/floordata')
        if(meta.status !== 200) return uni.$showMsg()
        message.forEach(item => {
          item.product_list.forEach(obj => {
          obj.url = `/subpkg/goods_list/goods_list?${obj.navigator_url.split('?')[1]}`
          })
        })
        this.floorList = message
      },
      // 搜索
      onSearch() {
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
      }
    },
    components:{
      MySearch
    }
  }
</script>

<style scoped lang="scss">
  .search-fix {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  swiper {
    height: 330rpx;
    .swiper-item, .nav-item, image {
      width: 100%;
      height: 100%;
    }
  }
  .nav-cate {
      display: flex;
      margin: 15px 0;
    .nav-content {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }
  .floor-item {
    .floor-title {
      height: 60rpx;
      width: 100%;
      margin-top: 4px;
    }
    .img-visible {
      display: flex;
      .left-img {
        display: flex;
        align-items: center;
        margin-left: 4px;
      }
      .right-img {
        padding-left: 10rpx;
        display: flex;
        flex-flow: wrap;
        justify-content: space-around;
        align-content: space-between;
        .right-img-item {
          height: 48%;
          image {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
</style>
